﻿using UnityEngine;
using System.Collections.Generic;
using RenderHeads.Media.AVProVideo;

public class MoviePlayerScript : MonoBehaviour
{
    public static MoviePlayerScript mInstance;

    public SelectPanelScript SelectPanel;
    public MediaPlayer MediaPlayer;
    public UITexture MovieTex;
    public GameObject StartBtn;
    public UILabel StartLabel;
    public GameObject RestartBtn;
    public GameObject LeftDownArrow;
    public GameObject LeftUpArrow;
    public GameObject Center;
    public GameObject RightUpArrow;
    public GameObject RightDownArrow;

    private UITweener[] LeftDownArrowTween;
    private UITweener[] LeftUpArrowTween;
    private UITweener[] CenterTween;
    private UITweener[] RightUpArrowTween;
    private UITweener[] RightDownArrowTween;
    private List<KeyInfo> KeyInfoList;

    void Awake()
    {
        mInstance = this;
    }
    void OnDestroy()
    {
        mInstance = null;
    }

    void Start()
    {
        Application.targetFrameRate = 30;
        UIEventListener.Get(StartBtn).onClick += OnBtnClick;
        UIEventListener.Get(RestartBtn).onClick += OnBtnClick;

        LeftDownArrowTween = LeftDownArrow.GetComponents<UITweener>();
        LeftUpArrowTween = LeftUpArrow.GetComponents<UITweener>();
        CenterTween = Center.GetComponents<UITweener>();
        RightUpArrowTween = RightUpArrow.GetComponents<UITweener>();
        RightDownArrowTween = RightDownArrow.GetComponents<UITweener>();

        KeyInfoList = new List<KeyInfo>();
        KeyInfo info = new KeyInfo();
        info.KeyCodeList = new List<KeyCode>() { KeyCode.Q, KeyCode.Joystick1Button6 };
        info.TweenerList = LeftUpArrowTween;
        KeyInfoList.Add(info);

        info = new KeyInfo();
        info.KeyCodeList = new List<KeyCode>() { KeyCode.E, KeyCode.Joystick1Button7 };
        info.TweenerList = RightUpArrowTween;
        KeyInfoList.Add(info);

        info = new KeyInfo();
        info.KeyCodeList = new List<KeyCode>() { KeyCode.S };
        info.TweenerList = CenterTween;
        info.needAxis = true;
        KeyInfoList.Add(info);

        info = new KeyInfo();
        info.KeyCodeList = new List<KeyCode>() { KeyCode.Z, KeyCode.Joystick1Button4 };
        info.TweenerList = LeftDownArrowTween;
        KeyInfoList.Add(info);

        info = new KeyInfo();
        info.KeyCodeList = new List<KeyCode>() { KeyCode.C, KeyCode.Joystick1Button5 };
        info.TweenerList = RightDownArrowTween;
        KeyInfoList.Add(info);
    }
    public void OnBtnClick(GameObject go)
    {
        if (go == StartBtn)
        {
            if (MediaPlayer.Control.IsPlaying())
            {
                MediaPlayer.Pause();
                StartLabel.text = "开始";
            }
            else
            {
                MediaPlayer.Play();
                StartLabel.text = "暂停";
            }
        }
        else if (go == RestartBtn)
        {
            gameObject.SetActive(false);
            SelectPanel.gameObject.SetActive(true);
            SelectPanel.Refresh();
            StartLabel.text = "暂停";
        }
    }
    void Update()
    {
        MovieTex.mainTexture = MediaPlayer.TextureProducer.GetTexture();
        for (int i = 0; i < KeyInfoList.Count; i++)
        {
            KeyInfoList[i].Check();
        }
        if (Input.GetKeyUp(KeyCode.Joystick1Button8))
        {
            OnBtnClick(RestartBtn);
        }
        if (Input.GetKeyUp(KeyCode.Joystick1Button9))
        {
            OnBtnClick(StartBtn);
        }
        //Debug.LogError(Input.GetAxis("GCVertical") + " " + Input.GetAxis("GCHorizontal") + " " + Input.GetAxis("GCTest1") + " " + Input.GetAxis("GCTest2"));
        //for (KeyCode k = KeyCode.Joystick1Button0; k <= KeyCode.Joystick1Button19; k++)
        //{
        //    if (Input.GetKeyDown(k))
        //    {
        //        Debug.LogError(k);
        //    }
        //}
    }
}
public class KeyInfo
{
    public List<KeyCode> KeyCodeList;
    public UITweener[] TweenerList;
    public bool needAxis = false;
    private float mPressTime;
    private bool bIsPressing;
    private const float mPressInterval = 0.2f;

    public void Check()
    {
        if (!bIsPressing && (GetDown() || needAxis && GetAxis()))
        {
            foreach (UITweener tw in TweenerList)
            {
                tw.ResetToBeginning();
                tw.PlayForward();
            }
            mPressTime = 0;
            bIsPressing = true;
        }
        if (bIsPressing && (GetUp() || needAxis && !GetAxis()))
        {
            bIsPressing = false;
        }
        if (bIsPressing)
        {
            mPressTime += Time.deltaTime;
            if (mPressTime >= mPressInterval)
            {
                mPressTime -= mPressInterval;
                foreach (UITweener tw in TweenerList)
                {
                    tw.ResetToBeginning();
                    tw.PlayForward();
                }
            }
        }
    }
    public bool GetDown()
    {
        foreach (var k in KeyCodeList)
        {
            if (Input.GetKeyDown(k))
            {
                return true;
            }
        }
        return false;
    }
    public bool GetUp()
    {
        foreach (var k in KeyCodeList)
        {
            if (Input.GetKeyUp(k))
            {
                return true;
            }
        }
        return false;
    }
    public bool GetAxis()
    {
        if (Input.GetAxis("GCHorizontal") > 0.5f)
        {
            return true;
        }
        return false;
    }
}
